package com.wugh.fast.security.social.view;

import com.wugh.fast.kernel.core.props.FastProperties;
import com.wugh.fast.kernel.core.props.SocialSecurityProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.servlet.view.RedirectView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

/**
 * 说明: 解绑和绑定后的视图处理
 * <pre>
 *     目前使用重定向的解决方式
 * </pre>
 *
 * @author wugh
 * @date 2018/9/18 11:26
 */
public class ConnectView extends RedirectView {
    private static final String DUPLICATE_CONNECTION_ATTRIBUTE = "social_addConnection_duplicate";
    private static final String PROVIDER_ERROR_ATTRIBUTE = "social_provider_error";
    private static final String AUTHORIZATION_ERROR_ATTRIBUTE = "social_authorization_error";
    private static final String DYNAMIC_URL = "DYNAMIC_URL";
    @Autowired
    private FastProperties fastProperties;

    public ConnectView() {
        super("");
    }

    @Override
    protected void renderMergedOutputModel(Map<String, Object> model, HttpServletRequest request, HttpServletResponse response) throws IOException {
        SocialSecurityProperties social = fastProperties.getSecurity().getSocial();
        Object connections = model.remove("connections");
        Object duplicate = model.remove(DUPLICATE_CONNECTION_ATTRIBUTE);
        Object provider = model.remove(PROVIDER_ERROR_ATTRIBUTE);
        Object authorization = model.remove(AUTHORIZATION_ERROR_ATTRIBUTE);
        Map<String, Object> params = new HashMap<>();
        params.put(DYNAMIC_URL, connections == null ? social.getUnbindSuccessUrl() : social.getBindSuccessUrl());
        String error = null;
        if (duplicate != null) {
            error = Objects.equals(duplicate, true) ? "社交账号重复绑定" : null;
        } else if (provider != null) {
            error = provider.toString();
        } else if (authorization != null) {
            error = authorization.toString();
        }
        if (error != null) {
            params.put("error", error);
        }
        super.renderMergedOutputModel(params, request, response);
    }

    /**
     * 重写原来的query属性设置,跳过DYNAMIC_URL属性,不处理
     *
     * @param targetUrl
     * @param model
     * @param encodingScheme
     * @throws UnsupportedEncodingException
     */
    @Override
    protected void appendQueryProperties(StringBuilder targetUrl, Map<String, Object> model, String encodingScheme) throws UnsupportedEncodingException {
        Object dynamicUrl = model.remove(DYNAMIC_URL);
        super.appendQueryProperties(targetUrl, model, encodingScheme);
        model.put(DYNAMIC_URL, dynamicUrl);
    }

    /**
     * 更新URL,根据DYNAMIC_URL,把动态url添加到url中
     *
     * @param targetUrl
     * @param model
     * @param request
     * @param response
     * @return
     */
    @Override
    protected String updateTargetUrl(String targetUrl, Map<String, Object> model, HttpServletRequest request, HttpServletResponse response) {
        String dynamicUrl = (String) model.get(DYNAMIC_URL);
        return dynamicUrl + super.updateTargetUrl(targetUrl, model, request, response);
    }
}

